/**
 * @filename:ContractDetailController 2021-11-09 01:40:36
 * @project com/lwl/blue  V1.0
 * Copyright(c) 2020 <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a> Co. Ltd.
 * All right reserved.
 */
package com.lwl.blue.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwl.blue.commont.base.*;
import com.lwl.blue.commont.utils.UserContextUtil;
import com.lwl.blue.entity.ContractDetail;
import com.lwl.blue.service.ContractDetailService;
import com.lwl.blue.service.FilesService;
import com.lwl.blue.vo.GetSupplierByContractIdVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>自定义方法写在这里</p>
 *
 * <p>说明： 合同明细API接口层</P>
 *
 * @version: V1.0
 * @author: <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a>
 */
// @Api(tags = "合同明细" )
@RestController
@RequestMapping("/contractDetail")
@Slf4j
public class ContractDetailController extends BaseController {
    @Autowired
    ContractDetailService contractDetailService;
    @Resource
    FilesService filesService;

    @PostMapping("/pages")
    public PageResult<ContractDetail> getPages(@RequestBody PageParam<ContractDetail> pageParam) {
        JsonResult<Page<ContractDetail>> pages = contractDetailService.pages(pageParam);
        PageResult<ContractDetail> re = new PageResult<>();
        re.success(pages.getData().getTotal(), pages.getData().getRecords());
        return re;
    }

    @PostMapping("/pagesDetail")
    public PageResult<ContractDetail> pagesDetail(@RequestBody PageParam<ContractDetail> pageParam) {
        PageResult<ContractDetail> re = new PageResult<>();
        if (null == pageParam.getParam() || null == pageParam.getParam().getContractId()) {
            re.success(0L, new ArrayList<>());
            return re;
        }
        JsonResult<Page<ContractDetail>> pages = contractDetailService.pages(pageParam);
        re.success(pages.getData().getTotal(), pages.getData().getRecords());
        return re;
    }

    @PostMapping("saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody ContractDetail contractDetail) {
        setInsetOrUpdateUser(contractDetail);
        ContractDetail old = null;
        if (Objects.nonNull(contractDetail.getId())) {
            old = contractDetailService.getById(contractDetail.getId());
        }
        contractDetailService.saveOrUpdate(contractDetail);
        filesService.updateBySaveOrUpdate(contractDetail.getPhoto(), Objects.isNull(old) ? null : old.getPhoto(), contractDetail.getId(), contractDetail.getItemNo());
        return new JsonResult().success("请求成功");
    }

    @PostMapping("/delete/{id}")
    public JsonResult saveOrUpdate(@PathVariable(value = "id") Integer id) {
        contractDetailService.update(new UpdateWrapper<ContractDetail>().lambda()
                .set(ContractDetail::getIsDelete, Const.IS_DELETE_YES)
                .set(ContractDetail::getUpdateUser, UserContextUtil.getUser().getUserName())
                .eq(ContractDetail::getId, id));
        ContractDetail byId = contractDetailService.getById(id);
        filesService.updateByDelete(byId.getPhoto());
        return new JsonResult().success("请求成功");
    }


    @PostMapping("/getSupplierByContractId/{contractId}")
    public JsonResult<List<GetSupplierByContractIdVO>> getSupplierByContractId(@PathVariable(value = "contractId") Integer id) {
        return contractDetailService.getSupplierByContractId(id);
    }
}
